package com.yuncheng.spcyApi.webOffice;

import com.yuncheng.annotation.AutoLog;
import com.yuncheng.spcyApi.entity.SpcyRsWjmb;
import com.yuncheng.spcyApi.service.ISpcyRsWjmbService;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "文件模板库-WebOffice在线文档编辑接口")
@RestController
@RequestMapping("/api/spzx-spcy/wjmbWebOfficeApi")
public class WjmbWebOfficeApi {

    private static final Logger log = LoggerFactory.getLogger(WjmbWebOfficeApi.class);

    @Resource
    private WebOfficeFile webOfficeFIle;

    @Resource
    private ISpcyRsWjmbService spcyRsWjmbService;

    @AutoLog(value = "服务端-打开模板在线文档-可编辑")
    @ApiOperation(value = "服务端-打开模板在线文档-可编辑")
    @GetMapping(value = "openFile")
    public HttpResult<?> openFile(@RequestParam(value = "id",defaultValue = "") String id){
        /*SpcyRsWjmb wjjl = spcyRsWjmbService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件不存在");
        }
        if (StringUtils.isNotBlank(wjjl.getFwjlx()) && wjjl.getFwjlx().equals(".pdf")){
            return HttpResult.error("当前文档为pdf文档，请选择重新编辑文档");
        }
        String url = webOfficeFIle.openFileByWjmb(wjjl,wjjl.getId(),wjjl.getFcflj(),1, 0);*/
        Map resultMap = new HashMap();
        resultMap.put("wordUrl", "");
        return HttpResult.ok(resultMap);
    }

    @AutoLog(value = "服务端-打开模板在线文档-只读")
    @ApiOperation(value = "服务端-打开模板在线文档-只读")
    @GetMapping(value = "openFileRead")
    public HttpResult<?> openFileRead(@RequestParam(value = "id",defaultValue = "") String id){
        /*SpcyRsWjmb wjjl = spcyRsWjmbService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件不存在");
        }
        if (StringUtils.isNotBlank(wjjl.getFwjlx()) && wjjl.getFwjlx().equals(".pdf")){
            return HttpResult.error("当前文档为pdf文档，请选择重新编辑文档");
        }
        String url = webOfficeFIle.openFileByWjmb(wjjl,wjjl.getId(),wjjl.getFcflj(),1, 1);*/
        Map resultMap = new HashMap();
        resultMap.put("wordUrl", "");
        return HttpResult.ok(resultMap);
    }

    @AutoLog(value = "获取APP-打开模板在线文档-只读")
    @ApiOperation(value = "获取APP-打开模板在线文档-只读")
    @GetMapping(value = "openAppFileRead")
    public HttpResult<?> openAppFileRead(@RequestParam(value = "id",defaultValue = "") String id){
        /*SpcyRsWjmb wjjl = spcyRsWjmbService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件不存在");
        }
        if (StringUtils.isNotBlank(wjjl.getFwjlx()) && wjjl.getFwjlx().equals(".pdf")){
            return HttpResult.error("当前文档为pdf文档，请选择重新编辑文档");
        }
        String url = webOfficeFIle.openFileByWjmb(wjjl,wjjl.getId(),wjjl.getFcflj(),1, 1, true);*/
        Map resultMap = new HashMap();
        resultMap.put("wordUrl", "");
        return HttpResult.ok(resultMap);
    }

    @AutoLog(value = "服务端-将word文件关闭并保存")
    @ApiOperation(value = "服务端-将word文件关闭并保存")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id",value = "文件id",required = true),
            @ApiImplicitParam(name = "isClose",value = "是否关闭文档，true=【关闭】")
    })
    @GetMapping(value = "getWebOfficeDownloadFile")
    public HttpResult<?> getWebOfficeDownloadFile(@RequestParam("id") String id,
                                                  @RequestParam(value = "isClose",defaultValue = "false") boolean isClose){
       /* boolean isDel = false;
        SpcyRsWjmb wjjl = spcyRsWjmbService.getById(id);
        if (wjjl == null){
            return HttpResult.error("文件不存在");
        }
        if (StringUtils.isNotBlank(wjjl.getFwjlx()) && wjjl.getFwjlx().equals(".pdf")){
            return HttpResult.ok("保存关闭成功");
        }

        // 关闭文档
        Result closeResult1 = webOfficeFIle.closeFile(id);
        if (!closeResult1.isSuccess()){
            return HttpResult.error(closeResult1.getMessage());
        }

        webOfficeFIle.getWebOfficeDownloadFile2ByWjmb(wjjl, wjjl.getFcflj(), isDel);

        // 关闭文档
        if (isClose) {
            return HttpResult.ok("保存关闭成功");
        }else {

            return HttpResult.ok("保存成功");
        }*/

        return HttpResult.ok("服务不可用！");
    }
}
